# encoding: utf-8

# 获取自己所有预测推荐(微服务接口,支持分页)

module Capevaluation
  module Business
    module Prediction

      #curl http://127.0.0.1:5678/api -d cmd=predict_get_submits -d type=0 -d player="FBA2017006" -d gzip=0
      #curl http://127.0.0.1:5678/api -d cmd=predict_get_submits -d type=0 -d player="FBE2017004" -d page=2 -d count=5 -d gzip=0
      #curl http://127.0.0.1:5678/api -d cmd=predict_get_submits -d type=1 -d player="13900312435" -d gzip=0
      def predict_get_submits(params)
        #self.logger.debug(CaiQiu::Support::Utils.get_backtrace_list)
        self.logger.debug(__method__.to_s + " params:#{params}")

        #1.检验参数是否为空
        if params['type'].nil? || params['player'].nil?
          raise Capevaluation::Exception::LackOfParams.new
        end

        #2.验证参数有效性
        #验证type字段
        type = params['type'].to_i
        raise Capevaluation::Exception::InvalidParams.new if !type.zero? && type!=1

        #3. 从redis中获取结果
        submit_results = self.redis.get("capevaluation.submit.#{params['player']}")
        if !submit_results.nil?
          return {:code => 0, :msg => '', :resp => [submit_results]}
          #return JSON.parse(ranking_results)
        end

        current_page = params['page']
        count = params['count']
        if current_page.nil?
          current_page = Capevaluation::Constant::Consumer::FIRST_PAGE
        end
        if count.nil?
          count = Capevaluation::Constant::Consumer::PAGE_COUNT
        end

        #4.整理参数
        params_new = {
            :type => params['type'],
            :player => params['player'],
            :page => current_page.to_i,
            :count => count.to_i
        }

        #5.得到结果
        player_submits,total_page = Capevaluation::Service::Submits.get_records_by_params(params_new)
        prediction = []
        result_count = 0
        player_submits.each do|player_submit|
          match_id = player_submit[:match_id]
          play_code = player_submit[:play_code]

          #5.1 得到比赛主客队的队名
          team_message = get_host_away_name(match_id)

          #5.2 得到玩法的名称
          play_info = check_play_code(play_code)
          play_info_map = JSON.parse play_info
          play_name = play_info_map['name']

          #5.3 翻译预测内容：3 -> 胜，1 -> 平，0 -> 负
          player_prediction = player_submit[:prediction].split(',')
          prediction_message = ""
          player_prediction.each do|one_prediction|
            case one_prediction
              when '3' then prediction_message += "胜，"
              when '1' then prediction_message += "平，"
              when '0' then prediction_message += "负，"
            end
          end
          prediction_message = prediction_message[0..prediction_message.size-2]

          #5.4 翻译复盘内容：1 -> 预测正确，0 -> 预测错误，9 -> 待定
          matching = player_submit[:matching]
          matching_message = ""
          case matching
            when 1 then matching_message = "预测正确"
            when 0 then matching_message = "预测错误"
            when 9 then matching_message = "待定"
          end

          prediction_in = {
              "match_id" => match_id,
              "vs" => team_message,
              "play_code" => play_name,
              "prediction" => prediction_message,
              "matching" => matching_message,
              "create_time" => player_submit[:create_time]
          }
          prediction.push prediction_in
        end

        #5.5 得到总页数

        resp = {
            "player" => params['player'],
            "page" => current_page.to_i+1,    #当前页数
            "total_page" => total_page, #总页数
            "prediction" => prediction
        }
        result = {:code => 0, :msg => '', :resp => [resp]}
        self.redis.setex("capevaluation.submit.#{params['player']}", 30, resp)
        return result
      end



    end
  end
end

